<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <jsp:include page="../inc/meta.jsp"></jsp:include>
    <jsp:include page="../inc/easyui.jsp"></jsp:include>
  </head>
  
  <body class="easyui-layout" fit="true">
    <div id="content" region="center" style="overflow: hidden">
        <div id="toolbar" class="datagrid-toolbar" style="height: 28px;">
            <div style="height: 28px;">

                <a class="easyui-linkbutton" iconcls="icon-add" onclick="createRole();" plain="true"
                    href="javascript:void(0);" style="float: left;">创建</a>
                <div class="datagrid-btn-separator">
                </div>
                <a class="easyui-linkbutton" iconcls="icon-edit" onclick="editRole();" plain="true"
                    href="javascript:void(0);" style="float: left;">编辑</a>
                <div class="datagrid-btn-separator">
                </div>
                <a class="easyui-linkbutton" iconcls="icon-remove" onclick="deleteRole();" plain="true"
                    href="javascript:void(0);" style="float: left;">删除</a>
                <div class="datagrid-btn-separator">
                </div>
                <a class="easyui-linkbutton" iconcls="icon-undo" onclick="roleGrid.datagrid('unselectAll');"
                    plain="true" href="javascript:void(0);" style="float: left;">取消选中</a>
            </div>
        </div>
        <table id="roleList" fit="true" border="false">
        </table>
    </div>
    <script type="text/javascript">
        var roleGrid;
        var url='<%=request.getContextPath()%>/role/GetList';
        $(function () {
            roleGrid = $('#roleList').datagrid({
                url: url,
                title: '角色列表',
                rownumbers: true,
                fitColumns:true,
                idField: 'id',
                toolbar: '#toolbar',
                pageSize: 10,
                pagination: true,
                pageList: [10, 20, 30, 40, 50, 60, 70, 80, 90, 100],
                frozenColumns: [[{
                    title: 'id',
                    field: 'Iid',
                    width: 50,
                    checkbox: true
                }, {
                    field: 'name',
                    title: '角色名',
                    width: 120,
                    sortable: true
                }]],
                columns: [[{
                    field: 'description',
                    title: '描述',
                    width: 200
                }, {
                    field: 'createDate',
                    title: '创建时间',
                    width: 120
                }, {
                    field: 'editPermission',
                    title: '编辑权限',
                    width: 80,
                    formatter: function (value, row, index) {
                        return "<a href=\"javascript:doEditPermission('" + row.id +  "');\"" + " title='编辑权限'>编辑权限</a>";
                    }
                }]]
            });

        });

        function doEditPermission(roleId) {
            var url = '<%=request.getContextPath()%>/role/toEditPermission?roleId=' + roleId;
            top.showMyWindow('编辑角色权限', 'icon-edit', url, 830, 480, true, false, true);
        }
        function viewRole(){
            var rows = roleGrid.datagrid('getSelections');
            if (rows.length > 0) {
                if (rows.length == 1) {
                    var url = "/Role/Details/" + rows[0].Id;
                    //添加新页面
                    top.addTab("系统角色详细", url, "icon-search");
                } else {
                    var names = [];
                    for (var i = 0; i < rows.length; i++) {
                        names.push(rows[i].Name);
                    }
                    showMsg("提示", '只能选择一个系统角色进行查看！您已经选择了【' + names.join(',') + '】' + rows.length + '个系统角色');
                }
            }
            else {
                showMsg("提示", "请选择要查看的系统角色！");
            }
        }
        //创建系统角色
        function createRole(){
        	 var fromUrl = '<%=request.getContextPath()%>/role/toCreateRole';
        	 var toUrl = '<%=request.getContextPath()%>/role/createRole';
             var p = top.sy.dialog({
                 title: '创建系统角色',
                 iconCls: 'icon-add',
                 href: fromUrl,
                 width: 450,
                 height: 400,
                 modal: true,
                 minimizable: false,
                 maximizable: false,
                 cache: false,
                 buttons: [{
                     text: '确定',
                     iconCls: 'icon-ok',
                     handler: function () {
                         var f = p.find('form');
                         f.form('submit', {
                             url: toUrl,
                             onsubmit: function () {
                                 var flag = $(this).form('validate');
                                 if (flag) {
                                     top.showProcess(true, '温馨提示', '正在提交数据...');
                                 }
                                 return flag;
                             },
                             success: function (data) {
                                 top.showProcess(false);
                                 var map = $.parseJSON(data);
                                 if (map.success) {
                                     top.showMsg('提示', map.msg);
                                     reloadDataGrid();
                                     p.dialog('close');
                                 }
                                 else {
                                     top.alertMsg('错误', map.msg+"\n"+(map.obj==null?"":map.obj));
                                 }

                             },
                             onLoadError: function () {
                                 top.showProcess(false);
                                 top.$.messager.alert('温馨提示', '由于网络或服务器太忙，提交失败，请重试！');
                             }
                         });

                     }
                 }, {
                     text: '取消',
                     iconCls: 'icon-cancel',
                     handler: function () {
                         p.dialog('close');
                     }
                 }]
             });

        }
        
        //编辑系统角色
        function editRole(){
            var rows = roleGrid.datagrid('getSelections');
            if (rows.length > 0) {
                if (rows.length == 1) {
                    var url='<%=request.getContextPath()%>/role/toEditRole?roleId='+rows[0].id;
                     var p = top.sy.dialog({
                         title: '编辑系统角色',
                         iconCls: 'icon-edit',
                         href: url,
                         width: 450,
                         height: 400,
                         modal: true,
                         minimizable: false,
                         maximizable: false,
                         cache: false,
                         buttons: [{
                             text: '确定',
                             iconCls: 'icon-ok',
                             handler: function () {
                                 var f = p.find('form');
                                 f.form('submit', {
                                     url: '<%=request.getContextPath()%>/role/editRole?roleId='+rows[0].id,
                                     onsubmit: function () {
                                         var flag = $(this).form('validate');
                                         if (flag) {
                                             top.showProcess(true, '温馨提示', '正在提交数据...');
                                         }
                                         return flag;
                                     },
                                     success: function (data) {
                                         top.showProcess(false);
                                         var map = $.parseJSON(data);
                                         if (map.success) {
                                             top.showMsg('提示', map.msg);
                                             reloadDataGrid();
                                             p.dialog('close');
                                         }
                                         else {
											top.alertMsg('错误', map.msg+"\n"+(map.obj==null?"":map.obj));
                                         }

                                     },
                                     onLoadError: function () {
                                         top.showProcess(false);
                                         top.$.messager.alert('温馨提示', '由于网络或服务器太忙，提交失败，请重试！');
                                     }
                                 });
                             }
                         }, {
                             text: '取消',
                             iconCls: 'icon-cancel',
                             handler: function () {
                                 p.dialog('close');
                             }
                         }]
                     });
                }
                else {
                    var names = [];
                    for (var i = 0; i < rows.length; i++) {
                        names.push(rows[i].name);
                    }
                    top.showMsg("提示", '只能选择一个角色进行编辑！您已经选择了【'+names.join(',')+'】'+rows.length+'个角色');
                }
            }
            else {
                top.showMsg("提示", "请选择要编辑的记录！");
            }
        }
        //删除系统角色
        function deleteRole() {
            var ids = [];
            var rows = roleGrid.datagrid('getSelections');
            if (rows.length>0) {
                $.messager.confirm('提示', '确定删除吗？', function (r) {
                    if (r) {
                        for (var i = 0; i < rows.length; i++) {
                            ids.push(rows[i].id);
                        }
                        $.post('<%=request.getContextPath()%>/role/deleteRole', { "roleIds": ids.join(',') }, function (data) {
                            if (data.success) {
                                reloadDataGrid();
                                top.showMsg('提示', data.msg);
                            }
                            else {
                                top.alertMsg('错误', data.msg+"\n"+(data.obj==null?"":data.obj));
                            }

                        }, "json");
                    }
                });
            }
            else {
                top.showMsg("提示", "请选择要删除的记录！");

            }
        }

        function reloadDataGrid() {
            roleGrid.datagrid('unselectAll');
            roleGrid.datagrid('reload');
        }
        
        
    </script>
</body>
</html>
